package models

import (
	"regexp"
	"time"
)

func PoemHrefList(context string) []string {
	list := make([]string, 0)

	reg := `<span><a href="/shiwenv_(.*?).aspx".*?>(.*?)</span>`

	reg1 := regexp.MustCompile(reg)
	if reg1 == nil {
		return nil
	}

	result1 := reg1.FindAllStringSubmatch(context, -1)

	for _, v := range result1 {
		href := "https://so.gushiwen.cn/shiwenv_"
		href = href + v[1] + ".aspx"
		list = append(list, href)
	}

	return list
}

func PoemInfo(context string) int64 {
	info := Info{}

	// 获取诗的标题
	titleReg := `<h1 style="font-size:20px; line-height:22px; height:22px; margin-bottom:10px;">(.*?)</h1>`
	info.Title = commonReg(context, titleReg, "匹配标题正则错误")

	// 获取诗人姓名
	authorReg := `<p class="source"><a.*?>(.*?)</a>`
	info.Author = commonReg(context, authorReg, "匹配诗人正则错误")

	// 匹配行文&注释
	proseReg := `<span id="fanyiPlay[\s\S]*?</div>([\s\S]*?)</div>`
	info.Prose = commonReg(context, proseReg, "匹配行文&注释正则错误")

	// 匹配赏析
	descReg := `<span id="shangxiPlay[\s\S]*?</div>([\s\S]*?)</div>`
	info.Desc = commonReg(context, descReg, "匹配赏析正则错误")

	// 诗词内容
	contentReg := `<p class="source">[\s\S]*?<div class="contson".*?>([\s\S]*?)</div>`
	info.Content = commonReg(context, contentReg, "匹配诗词内容正则错误")

	// 匹配图片
	imageReg := `<div class="divimg"[\s\S]*?<img src="(.*?)".*?>`
	info.Image = commonReg(context, imageReg, "匹配图片正则错误")

	now := time.Now()
	info.CreateTime = now.Format("2006-01-02 15:04:05")
	info.UpdateTime = now.Format("2006-01-02 15:04:05")

	return InsertInfo(info)
}

// 单匹配正则
func commonReg(context, reg, err string) string {
	regTitle := regexp.MustCompile(reg)
	if regTitle == nil {
		panic(err)
	}
	result := regTitle.FindStringSubmatch(context)
	if result != nil {
		return result[1]
	}
	return ""
}
